package neves.android.etmg.sprite;

import android.util.Log;
import neves.android.etmg.map.GameMap;
import neves.android.etmg.tilemap.TileMap;
import neves.android.etmg.tilemap.TileMapRoom;

// Kill on Sight creature controller
public class BaseController implements CreatureControl {

	@Override
	public void NextMove(GameMap curMap, Creature player, Creature curCreature) {

		int[] posForCreature = CaluateOrientation(curCreature, player);
		
		if(IsInPlayerRoom((TileMap)curMap, player, curCreature))
		{
			curCreature.UpdateTrackCount();
		}else if(IsOnSight((TileMap)curMap,curCreature, posForCreature[1],posForCreature[2] )){
			
		}
		if ( curCreature.IsTracking()) {
			
			curCreature.DecayTrackCount();
			
			
			
			if (IsNeighbor(posForCreature[1], posForCreature[2])) {
				// Attack player!!!

			} else {
				
				int[] shift = ShiftByDirection(posForCreature[0]);			
				
				//Log.w("ETMG", "Mob Move "+posForCreature[0]+" "+shift[0]+" "+shift[1]);
				
				curCreature.SetShift(shift[0], shift[1]);

			}

		} else { // fooling around
			RandomMove(curCreature);
		}
	}
	
	public void RandomMove(Creature curCreature){
		int ranDir = (int)((Math.random()*10) %8);
		
		int[] ranMove = ShiftByDirection(ranDir);
		
		curCreature.SetShift(ranMove[0], ranMove[1]);
		
		//Log.w("ETMG", "Mob RMove"+ranMove[0]+" "+ranMove[1]);
	}
	
	protected int[] ShiftByDirection( int direction){
		
		int[] shift = new int[2];
		
		switch (direction) {
		case 0:
			shift = new int[]{-1, 0};
			break;
		case 1:
			shift = new int[]{0, -1};
			break;
		case 2:
			shift = new int[]{1, 0};
			break;
		case 3:
			shift = new int[]{0, 1};
			break;
		case 4:
			shift = new int[]{-1, -1};
			break;
		case 5:
			shift = new int[]{1, -1};
			break;
		case 6:
			shift = new int[]{-1, 1};
			break;
		case 7:
			shift = new int[]{1, 1};
			break;
		default:
			break;
		}
		
		return shift;
	}

	protected boolean IsInPlayerRoom(TileMap curMap, Creature player,
			Creature curCreature) {

		TileMapRoom playerRoom = curMap.InWhichRoom(player.GetPos());
		if (playerRoom != null) {
			int[] cpos = curCreature.GetPos();
			if (playerRoom.IsInRoom(cpos[0], cpos[1])) {
				return true;
			}
		}

		return false;
	}

	protected boolean IsOnSight(TileMap curMap, Creature crature, int distx, int disty) {
		
		if( (distx ==0 && (Math.abs(disty)<5)) || (disty ==0 && (Math.abs(distx)<5)) ){
			boolean anyBarrier = false;
			//for(int temi = )
			
			return true;
		}
				
		return false;
	}	
	
	protected int[] CaluateOrientation(Creature ca, Creature cb) {

		// /
		// / 4 1 5
		// / 0 Ca 2
		// / 6 3 7
		// / {direction, x distance, y distance}

		int[] orientation = new int[3];

		int[] posA = ca.GetPos();
		int[] posB = cb.GetPos();

		orientation[1] = posA[0] - posB[0];
		orientation[2] = posA[1] - posB[1];

		if (orientation[1] == 0) {
			if (orientation[2] > 0) {
				orientation[0] = 1;
			} else {
				orientation[0] = 3;
			}
		} else if (orientation[2] == 0) {
			if (orientation[1] > 0) {
				orientation[0] = 0;
			} else {
				orientation[0] = 2;
			}
		} else {
			if (orientation[1] > 0) {
				if (orientation[2] > 0) {
					orientation[0] = 4;
				} else {
					orientation[0] = 6;
				}
			} else {
				if (orientation[2] > 0) {
					orientation[0] = 5;
				} else {
					orientation[0] = 7;
				}
			}
		}

		return orientation;
	}

	protected boolean IsNeighbor(int x_dis, int y_dis) {
		if (Math.abs(x_dis) < 2 && Math.abs(y_dis) < 2) {
			return true;
		}
		return false;
	}
}
